###################################################################################################
## Payson (2020) "The Partisan Logic of City Mobilization" Replication Code
## Mayoral Party Analysis (Table 4)
## Created with R version 3.4.1 (2017-06-30) -- "Single Candle"
###################################################################################################

## Set working directory
setwd()

## Load pacakges
library(lfe); library(stargazer)


## Functions for table tables (Note: tables will not compile without these functions)
pretty <- function(x) {
  paste(prettyNum(x, big.mark = ","))}

num.cities <- function(x) {
  length(which(getfe(x)=="fips"))}

N <- function(x) {x$N}

getmean <- function(x) {
  round(mean(x$response), 2)}


###################################################################################################

## load mayors.csv
dta <- read.csv("mayors.csv")


###################################################################################################
## Table 4: Effect of Mayoral Partisan Mismatch on City Lobbying
###################################################################################################


## Replicate effect of partisan mismatch on lobbying with mayoral subset
m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m)


## Mayor mismatch is positive but smaller and noisier
m1 <- felm(lobby ~ mayor.mismatch + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m1)


## Including both vars in same model: the voteshare tercile mismatch continues to dominate
m2 <- felm(lobby ~ mismatch + mayor.mismatch + log.pop + log.income + log.own.source +
             log.med.house | fips + state.by.year | 0 | fips, 
            data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m2)


stargazer(m, m1, m2, omit.table.layout = "s",  type = "text", star.cutoffs = c(0.05),
          title = c("Effect of Mayoral Partisan Mismatch on City Lobbying."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = c("mismatch", "mayor.mismatch"),
          covariate.labels = c("Partisan Mismatch", "Mayor Mismatch"),
          add.lines = list(c("City FEs", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")


